package com.shujia.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo1WordCount {

    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

        /**
         * map方法：每一行数据都会执行一次
         */
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {

            //获取一行数据
            String line = value.toString();

            //对这一行数据进行切分
            String[] words = line.split(",");

            //遍历得到切分的单词
            for (String word : words) {
                //将得到的单词输出到reduce端
                //同一个key会被分到同一个reduce中
                context.write(new Text(word), new LongWritable(1));
            }
        }
    }

    /**
     * reduce方法：每一个key调用一次
     * <p>
     * key：一个单词
     * values：一个单词所有的值（很多个1）
     */
    public static class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {

            //累计统计单词的数量
            Long sum = 0L;
            for (LongWritable value : values) {
                sum = sum + value.get();
            }

            //单词
            String word = key.toString();

            //将数据输出到hdfs
            context.write(new Text(word), new LongWritable(sum));

        }
    }

    public static void main(String[] args) throws Exception {

        //获取一个mapreduce任务
        Job job = Job.getInstance();

        //指定任务名称
        job.setJobName("wordcount");

        //指定reduce的数量，默认只有一个
        job.setNumReduceTasks(3);

        //指定main所在类的类名
        job.setJarByClass(Demo1WordCount.class);

        //指定map端的类
        job.setMapperClass(WordCountMapper.class);

        //指定map端输出的key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //指定reduce端的类
        job.setReducerClass(WordCountReducer.class);

        //指定reduce端输出的key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //指定输入路径，hdfs上的一个文件或者目录
        //先将文件 words.txt 上传到hdfs hadoop dfs -ls /word
        FileInputFormat.addInputPath(job, new Path("/word/words.txt"));

        Path outPath = new Path("/word/wc");
        FileSystem fileSystem = FileSystem.get(new Configuration());
        //如果输出路径已经存在，先删除
        if (fileSystem.exists(outPath)) {
            fileSystem.delete(outPath, true);
        }

        //指定输出路径，输出路径只能是一个目录
        FileOutputFormat.setOutputPath(job, new Path("/word/wc"));

        //启动任务
        job.waitForCompletion(true);
        /**
         * 提交任务
         * 1、通过maven将项目打包，上传到服务器
         * 2、提交任务，指定jar包 指定类全名
         * hadoop jar hadoop-1.0-SNAPSHOT.jar com.shujia.mapreduce.Demo1WordCount
         *
         * 3、查看结果
         * hadoop dfs -cat /word/wc/*
         *
         * 默认只有一个reduce所以只生成了一个文件
         *
         */
    }
}
